<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="就诊人" prop="patientName">
        <el-input disabled v-model="formData.patientName" placeholder="请输入就诊人姓名" />
      </el-form-item>
      <el-form-item label="第几次治疗" prop="num">
        <el-input v-model="formData.num" placeholder="请输入第几次治疗" />
      </el-form-item>
      <el-form-item label="化疗时间" prop="hlDate">
        <el-date-picker
          v-model="formData.hlDate"
          type="datetime"
          value-format="YYYY-MM-DD HH:mm:ss"
          placeholder="选择化疗时间"
        />
      </el-form-item>
      <el-form-item label="ALT" prop="alt">
        <el-input v-model="formData.alt" placeholder="请输入ALT" />
      </el-form-item>
      <el-form-item label="AST" prop="ast">
        <el-input v-model="formData.ast" placeholder="请输入AST" />
      </el-form-item>
      <el-form-item label="Cr" prop="cr">
        <el-input v-model="formData.cr" placeholder="请输入Cr" />
      </el-form-item>
      <el-form-item label="BUN" prop="bun">
        <el-input v-model="formData.bun" placeholder="请输入BUN" />
      </el-form-item>
      <el-form-item label="PSA" prop="psa">
        <el-input v-model="formData.psa" placeholder="请输入PSA" />
      </el-form-item>
      <el-form-item label="睾酮" prop="testosterone">
        <el-input v-model="formData.testosterone" placeholder="请输入睾酮" />
      </el-form-item>
      <el-form-item label="WBC" prop="wbc">
        <el-input v-model="formData.wbc" placeholder="请输入WBC" />
      </el-form-item>
      <el-form-item label="Hb" prop="hb">
        <el-input v-model="formData.hb" placeholder="请输入Hb" />
      </el-form-item>
      <el-form-item label="Plt" prop="plt">
        <el-input v-model="formData.plt" placeholder="请输入Plt" />
      </el-form-item>
      <el-form-item label="用药" prop="useMedication">
        <el-input v-model="formData.useMedication" placeholder="请输入用药" />
      </el-form-item>
      <el-form-item label="多西他赛" prop="dxts">
        <el-input v-model="formData.dxts" placeholder="请输入多西他赛" />
      </el-form-item>
      <el-form-item label="其它" prop="otherData">
        <el-input v-model="formData.otherData" placeholder="请输入其它" />
      </el-form-item>
      <el-divider><h2>副作用</h2></el-divider>
      <el-divider content-position="left" ><h3>恶心</h3></el-divider>
      <el-form-item label="分级" prop="fzyExFj">
        <el-input v-model="formData.fzyExFj" placeholder="请输入副作用-恶心-分级" />
      </el-form-item>
      <el-form-item label="辅助用药" prop="fzyExFzyy">
        <el-input v-model="formData.fzyExFzyy" placeholder="请输入副作用-恶心-辅助用药" />
      </el-form-item>
      <el-form-item label="恢复" prop="fzyExHf">
        <el-input v-model="formData.fzyExHf" placeholder="请输入副作用-恶心-恢复" />
      </el-form-item>
      <el-divider content-position="left"><h3>呕吐</h3></el-divider>
      <el-form-item label="分级" prop="fzyOtFj">
        <el-input v-model="formData.fzyOtFj" placeholder="请输入副作用-呕吐-分级" />
      </el-form-item>
      <el-form-item label="辅助用药" prop="fzyOtFzyy">
        <el-input v-model="formData.fzyOtFzyy" placeholder="请输入副作用-呕吐-辅助用药" />
      </el-form-item>
      <el-form-item label="恢复" prop="fzyOtHf">
        <el-input v-model="formData.fzyOtHf" placeholder="请输入副作用-呕吐-恢复" />
      </el-form-item>
      <el-divider content-position="left"><h3>腹泻</h3></el-divider>
      <el-form-item label="分级" prop="fzyFxFj">
        <el-input v-model="formData.fzyFxFj" placeholder="请输入副作用-腹泻-分级" />
      </el-form-item>
      <el-form-item label="辅助用药" prop="fzyFxFzyy">
        <el-input v-model="formData.fzyFxFzyy" placeholder="请输入副作用-腹泻-辅助用药" />
      </el-form-item>
      <el-form-item label="恢复" prop="fzyFxHf">
        <el-input v-model="formData.fzyFxHf" placeholder="请输入副作用-腹泻-恢复" />
      </el-form-item>
      <el-divider content-position="left"><h3>外周神经毒性</h3></el-divider>
      <el-form-item label="分级" prop="fzyWzsjdxFj">
        <el-input v-model="formData.fzyWzsjdxFj" placeholder="请输入副作用-外周神经毒性-分级" />
      </el-form-item>
      <el-form-item label="辅助用药" prop="fzyWzsjdxFzyy">
        <el-input v-model="formData.fzyWzsjdxFzyy" placeholder="请输入副作用-外周神经毒性-辅助用药" />
      </el-form-item>
      <el-form-item label="恢复" prop="fzyWzsjdxHf">
        <el-input v-model="formData.fzyWzsjdxHf" placeholder="请输入副作用-外周神经毒性-恢复" />
      </el-form-item>
      <el-divider content-position="left"><h3>骨髓抑制</h3></el-divider>
      <el-form-item label="分级" prop="fzyGsyzFj">
        <el-input v-model="formData.fzyGsyzFj" placeholder="请输入副作用-骨髓抑制-分级" />
      </el-form-item>
      <el-form-item label="辅助用药" prop="fzyGsyzFzyy">
        <el-input v-model="formData.fzyGsyzFzyy" placeholder="请输入副作用-骨髓抑制-辅助用药" />
      </el-form-item>
      <el-form-item label="恢复" prop="fzyGsyzHf">
        <el-input v-model="formData.fzyGsyzHf" placeholder="请输入副作用-骨髓抑制-恢复" />
      </el-form-item>
      <el-divider content-position="left"><h3>液体潴留</h3></el-divider>
      <el-form-item label="分级" prop="fzyYtzlFj">
        <el-input v-model="formData.fzyYtzlFj" placeholder="请输入副作用-液体潴留-分级" />
      </el-form-item>
      <el-form-item label="辅助用药" prop="fzyYtzlFzyy">
        <el-input v-model="formData.fzyYtzlFzyy" placeholder="请输入副作用-液体潴留-辅助用药" />
      </el-form-item>
      <el-form-item label="恢复" prop="fzyYtzlHf">
        <el-input v-model="formData.fzyYtzlHf" placeholder="请输入副作用-液体潴留-恢复" />
      </el-form-item>
      <el-divider content-position="left"><h3>其它</h3></el-divider>
      <el-form-item label="分级" prop="fzyOtherFj">
        <el-input v-model="formData.fzyOtherFj" placeholder="请输入副作用-其它-分级" />
      </el-form-item>
      <el-form-item label="辅助用药" prop="fzyOtherFzyy">
        <el-input v-model="formData.fzyOtherFzyy" placeholder="请输入副作用-其它-辅助用药" />
      </el-form-item>
      <el-form-item label="恢复" prop="fzyOtherHf">
        <el-input v-model="formData.fzyOtherHf" placeholder="请输入副作用-其它-恢复" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { PatientHlApi, PatientHl } from '@/api/business/patienthl'

/** 化疗 表单 */
defineOptions({ name: 'PatientHlForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  patientId: undefined,
  patientName: undefined,
  doctorId: undefined,
  doctorName: undefined,
  num: undefined,
  hlDate: undefined,
  alt: undefined,
  ast: undefined,
  cr: undefined,
  bun: undefined,
  psa: undefined,
  testosterone: undefined,
  wbc: undefined,
  hb: undefined,
  plt: undefined,
  useMedication: undefined,
  dxts: undefined,
  otherData: undefined,
  fzyExFj: undefined,
  fzyExFzyy: undefined,
  fzyExHf: undefined,
  fzyOtFj: undefined,
  fzyOtFzyy: undefined,
  fzyOtHf: undefined,
  fzyFxFj: undefined,
  fzyFxFzyy: undefined,
  fzyFxHf: undefined,
  fzyWzsjdxFj: undefined,
  fzyWzsjdxFzyy: undefined,
  fzyWzsjdxHf: undefined,
  fzyGsyzFj: undefined,
  fzyGsyzFzyy: undefined,
  fzyGsyzHf: undefined,
  fzyYtzlFj: undefined,
  fzyYtzlFzyy: undefined,
  fzyYtzlHf: undefined,
  fzyOtherFj: undefined,
  fzyOtherFzyy: undefined,
  fzyOtherHf: undefined,
})
const formRules = reactive({
  patientId: [{ required: true, message: '就诊人ID不能为空', trigger: 'blur' }],
  patientName: [{ required: true, message: '就诊人姓名不能为空', trigger: 'blur' }],
  doctorId: [{ required: true, message: '录入医生ID不能为空', trigger: 'blur' }],
  doctorName: [{ required: true, message: '录入医生姓名不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()

  formData.value.patientId = route.query.id
  formData.value.patientName = route.query.name
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await PatientHlApi.getPatientHl(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as PatientHl
    if (formType.value === 'create') {
      await PatientHlApi.createPatientHl(data)
      message.success(t('common.createSuccess'))
    } else {
      await PatientHlApi.updatePatientHl(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    patientId: undefined,
    patientName: undefined,
    doctorId: undefined,
    doctorName: undefined,
    num: undefined,
    hlDate: undefined,
    alt: undefined,
    ast: undefined,
    cr: undefined,
    bun: undefined,
    psa: undefined,
    testosterone: undefined,
    wbc: undefined,
    hb: undefined,
    plt: undefined,
    useMedication: undefined,
    dxts: undefined,
    otherData: undefined,
    fzyExFj: undefined,
    fzyExFzyy: undefined,
    fzyExHf: undefined,
    fzyOtFj: undefined,
    fzyOtFzyy: undefined,
    fzyOtHf: undefined,
    fzyFxFj: undefined,
    fzyFxFzyy: undefined,
    fzyFxHf: undefined,
    fzyWzsjdxFj: undefined,
    fzyWzsjdxFzyy: undefined,
    fzyWzsjdxHf: undefined,
    fzyGsyzFj: undefined,
    fzyGsyzFzyy: undefined,
    fzyGsyzHf: undefined,
    fzyYtzlFj: undefined,
    fzyYtzlFzyy: undefined,
    fzyYtzlHf: undefined,
    fzyOtherFj: undefined,
    fzyOtherFzyy: undefined,
    fzyOtherHf: undefined,
  }
  formRef.value?.resetFields()
}

const route = useRoute() // 路由
const router = useRouter() // 路由

/** 初始化 **/
onMounted(() => {
  // 如果就诊人 ID 不存在，显示错误提示并关闭页面
  if (!route.query.id) {
    message.error('就诊人 ID 不存在，无法查看就诊记录')
    // 关闭当前路由，返回到就诊人列表页面
    router.push({ name: 'UserPatient' })
    return
  }
})
</script>
